/**
 * @file server.cc
 * @author zziywang (zziywang@163.com)
 * @brief { life is too short to learn cpp! }
 * @version 2.0
 * @date 2022-05-07
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "Acceptor.hh"
#include "EventLoop.hh"
#include <iostream>
using namespace std;
using namespace snow;

// 自定义回调函数传参
void onConnection(const TcpConnectionPtr &ptr)
{
    cout << ptr->printTcp() << " has connected!" << endl;
    ptr->send("Welcome to EchoServer");
}
void onMessage(const TcpConnectionPtr &ptr)
{
    cout << ">> from client " << ptr->printTcp() << endl;
    string s = ptr->recvLine();
    cout << s << endl;
    ptr->send(s); // 回显
}
void onClose(const TcpConnectionPtr &ptr)
{
    cout << "client " << ptr->printTcp() << " has disconnected!" << endl;
}

int main()
{
    Acceptor acceptor("172.25.40.81", 9999);
    acceptor.ready();
    cout << "Acceptor is ready!" << endl;
    EventLoop loop(acceptor); // 传acceptor引用构造
    // 设置回调函数
    loop.setConnectionCallBack(onConnection);
    loop.setMessageCallBack(onMessage);
    loop.setCloseCallBack(onClose);
    loop.loop(); // 开始事件循环

    return 0;
}